/*
package org.linlinjava.litemall.admin.service.order_manage;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.linlinjava.litemall.admin.util.OrderStatusEnum;
import org.linlinjava.litemall.admin.vo.order_manage.*;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.dao.*;
import org.linlinjava.litemall.db.dao.order_manage.AgedMallOrderMapper;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.oder_manage.AgedMallOrderDbService;
import org.linlinjava.litemall.db.util.OrderUtil;
import org.linlinjava.litemall.db.vo.order_manage.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.util.*;

*/
/**
 * @author wj
 * @date 2021/5/27 18:53
 *//*

@Service
@Transactional(rollbackFor = Exception.class)
public class AgedMallOrderApiService {

    @Resource
    private AgedMallOrderDbService agedMallOrderDbService;

    @Resource
    private AgedmallLogisticsPackageInfoMapper agedmallLogisticsPackageInfoMapper;

    @Resource
    private LitemallOrderMapper litemallOrderMapper;

    @Resource
    private LitemallOrderGoodsMapper litemallOrderGoodsMapper;

    @Resource
    private AgedmallReturnGoodsRecordMapper agedmallReturnGoodsRecordMapper;

    @Resource
    private AgedmallPickupGoodsRecordMapper agedmallReturnGoodsRecord;

    @Resource
    private LitemallAftersaleMapper litemallAftersaleMapper;

    @Resource
    private AgedMallOrderMapper agedMallOrderMapper;

    @Resource
    private AgedmallStoreMapper agedmallStoreMapper;

    @Resource
    private LitemallSystemMapper litemallSystemMapper;

    @Resource
    private AgedmallDeductDetailMapper agedmallDeductDetailMapper;

    @Resource
    private AgedmallUserInfoMapper agedmallUserInfoMapper;


    public Object list(OrderSelectVo orderSelectVo) {
        LitemallAdmin admin = (LitemallAdmin) SecurityUtils.getSubject().getPrincipal();
        Page<Object> page = PageHelper.startPage(orderSelectVo.getPageNum(), orderSelectVo.getPageSize());
        List<OrderListBackVo> orderManageList = agedMallOrderDbService.getOrderManageList(admin.getPhone(), orderSelectVo.getStoreId(), orderSelectVo.getPhone(), orderSelectVo.getOrderStatus(), orderSelectVo.getStartTime(), orderSelectVo.getEndTime(),orderSelectVo.getIsAfterSale());
        for (OrderListBackVo orderListBackVo : orderManageList) {
            List<OrderGoodsBackVo> liteMallOrderGoods = agedMallOrderDbService.getOrderGoodsByStoreIdAndOrderId(orderListBackVo.getStoreId(), orderListBackVo.getId());
            for (int j = 0; j < liteMallOrderGoods.size(); j++) {
                OrderGoodsBackVo orderGoodsBackVo = liteMallOrderGoods.get(j);
                AgedmallLogisticsPackageInfoExample agedmallLogisticsPackageInfoExample = new AgedmallLogisticsPackageInfoExample();
                agedmallLogisticsPackageInfoExample.or().andOrderCodeEqualTo(litemallOrderMapper.selectByPrimaryKey(orderGoodsBackVo.getOrderId()).getOrderSn());
                List<AgedmallLogisticsPackageInfo> agedMallLogisticsPackageInfoList = agedmallLogisticsPackageInfoMapper.selectByExample(agedmallLogisticsPackageInfoExample);
                for (AgedmallLogisticsPackageInfo logisticsPackageInfo : agedMallLogisticsPackageInfoList) {
                    String[] orderGoodsId = logisticsPackageInfo.getOrderGoodsIds().split(",");
                    if (orderGoodsId.length > 0 && Arrays.asList(orderGoodsId).contains(orderGoodsBackVo.getId().toString())) {
                        orderGoodsBackVo.setShipDictName(logisticsPackageInfo.getShipDictName());
                        orderGoodsBackVo.setShipSn(logisticsPackageInfo.getShipSn());
                        orderGoodsBackVo.setShipChannel(logisticsPackageInfo.getShipChannel());
                        orderGoodsBackVo.setShipStatus(logisticsPackageInfo.getShipStatus());
                        orderListBackVo.setGoodsList(liteMallOrderGoods);
                    }
                }

            }
            orderListBackVo.setGoodsList(liteMallOrderGoods);
        }
        Map<String, Object> backMap = new HashMap<>(3);
        backMap.put("data", orderManageList);
        backMap.put("total", page.getTotal());
        return ResponseUtil.ok(backMap);
    }

    public Object getOrderDetail(Integer id) {
        LitemallOrder litemallOrder = litemallOrderMapper.selectByPrimaryKeySelective(id);
        OrderDetailBackVo orderDetailBackVo = new OrderDetailBackVo();
        BeanUtils.copyProperties(litemallOrder, orderDetailBackVo);
        Map<Integer, Object> packageMap = new LinkedHashMap<>();
        //物品列表
        List<OrderGoodsBackVo> liteMallOrderGoods = agedMallOrderDbService.getOrderGoodsByOrderId(orderDetailBackVo.getId());
        orderDetailBackVo.setGoodsList(liteMallOrderGoods);
        //分包裹
        AgedmallLogisticsPackageInfoExample agedmallLogisticsPackageInfoExample = new AgedmallLogisticsPackageInfoExample();
        agedmallLogisticsPackageInfoExample.or().andOrderCodeEqualTo(litemallOrderMapper.selectByPrimaryKey(litemallOrder.getId()).getOrderSn());
        List<AgedmallLogisticsPackageInfo> agedMallLogisticsPackageInfoList = agedmallLogisticsPackageInfoMapper.selectByExample(agedmallLogisticsPackageInfoExample);
        for (int k = 0; k < agedMallLogisticsPackageInfoList.size(); k++) {
            List<OrderGoodsBackVo> packageGoods = new ArrayList<>();
            OrderPackageVo orderPackageVo = new OrderPackageVo();
            String[] orderGoodsId;
            orderGoodsId = agedMallLogisticsPackageInfoList.get(k).getOrderGoodsIds().split(",");
            for (OrderGoodsBackVo orderGoodsBackVo : liteMallOrderGoods) {
                if (orderGoodsId.length > 0 && Arrays.asList(orderGoodsId).contains(orderGoodsBackVo.getId().toString())) {
                    AgedmallLogisticsPackageInfo agedmallLogisticsPackageInfo = agedMallLogisticsPackageInfoList.get(k);
                    if (agedmallLogisticsPackageInfo != null) {
                        orderGoodsBackVo.setShipDictName(agedmallLogisticsPackageInfo.getShipDictName());
                        orderGoodsBackVo.setShipSn(agedmallLogisticsPackageInfo.getShipSn());
                        orderGoodsBackVo.setShipChannel(agedmallLogisticsPackageInfo.getShipChannel());
                        orderGoodsBackVo.setShipStatus(agedmallLogisticsPackageInfo.getShipStatus());
                        orderPackageVo.setShipDictName(agedmallLogisticsPackageInfo.getShipDictName());
                        orderPackageVo.setShipSn(agedmallLogisticsPackageInfo.getShipSn());
                        orderPackageVo.setShipChannel(agedmallLogisticsPackageInfo.getShipChannel());
                        orderPackageVo.setShipStatus(agedmallLogisticsPackageInfo.getShipStatus());
                        orderPackageVo.setShipTime(agedmallLogisticsPackageInfo.getShipTime());
                        orderPackageVo.setPackageType(agedmallLogisticsPackageInfo.getPackageType());
                    }
                    packageGoods.add(orderGoodsBackVo);
                }
            }
            orderPackageVo.setGoodsBackVos(packageGoods);
            packageMap.put(k, orderPackageVo);
        }
        List<OrderLogisticsMessage> orderLogisticsMessageList =new ArrayList<>();
        for (int i = 0; i < liteMallOrderGoods.size(); i++) {
            OrderLogisticsMessage orderLogisticsMessage=new OrderLogisticsMessage();
            AgedmallStore agedmallStore=agedmallStoreMapper.selectByPrimaryKey(liteMallOrderGoods.get(i).getStoreId().longValue());
            orderLogisticsMessage.setStoreId(agedmallStore.getId().intValue());
            orderLogisticsMessage.setStoreName(agedmallStore.getStoreName());
            orderLogisticsMessage.setShipDictName(liteMallOrderGoods.get(i).getShipDictName());
            orderLogisticsMessage.setShipChannel(liteMallOrderGoods.get(i).getShipChannel());
            orderLogisticsMessage.setShipSn(liteMallOrderGoods.get(i).getShipSn());
            orderLogisticsMessage.setAddress(agedmallStore.getProvinceName()+agedmallStore.getCityName()+agedmallStore.getAreaName()+agedmallStore.getAddressDetail());
            orderLogisticsMessageList.add(orderLogisticsMessage);
        }
        orderDetailBackVo.setOrderLogisticsMessages(orderLogisticsMessageList);
        orderDetailBackVo.setPackageMap(packageMap);
        return ResponseUtil.ok(orderDetailBackVo);
    }


    public Object deliverGoods(DeliverGoodsVo deliverGoodsVo) {
        LitemallAdmin admin = (LitemallAdmin) SecurityUtils.getSubject().getPrincipal();
        //自提
        if(StringUtils.isNotBlank(deliverGoodsVo.getExtractCode())){
            agedMallOrderMapper.updateOrderByOrderSn(OrderStatusEnum.ORDER_DELIVER_GOODS.getOrderStatus(),LocalDateTime.now(),deliverGoodsVo.getSubOrderCode(),deliverGoodsVo.getExtractCode());

        }
        //正常发货
        else{
            List<AgedmallLogisticsPackageInfo> packageInfos=new ArrayList<>();
            for (int i = 0; i < deliverGoodsVo.getPackageGoodsVos().size(); i++) {
                AgedmallLogisticsPackageInfo agedmallLogisticsPackageInfo=new AgedmallLogisticsPackageInfo();
                PackageGoodsVo packageGoodsVo = deliverGoodsVo.getPackageGoodsVos().get(i);
                agedmallLogisticsPackageInfo.setOrderCode(deliverGoodsVo.getOrderCode());
                agedmallLogisticsPackageInfo.setSubOrderCode(deliverGoodsVo.getSubOrderCode());
                agedmallLogisticsPackageInfo.setOrderGoodsIds(packageGoodsVo.getOrderGoodsIds());
                agedmallLogisticsPackageInfo.setGoodsIds(packageGoodsVo.getGoodsIds());
                agedmallLogisticsPackageInfo.setPackageType(packageGoodsVo.getPackageType().byteValue());
                agedmallLogisticsPackageInfo.setShipDictName(packageGoodsVo.getShipDictName());
                agedmallLogisticsPackageInfo.setShipSn(packageGoodsVo.getShipSn());
                agedmallLogisticsPackageInfo.setShipChannel(packageGoodsVo.getShipChannel());
                agedmallLogisticsPackageInfo.setShipTime(LocalDateTime.now());
                agedmallLogisticsPackageInfo.setAddTime(LocalDateTime.now());
                agedmallLogisticsPackageInfo.setOptUid(admin.getId());
                agedmallLogisticsPackageInfo.setShipStatus(0);
                agedmallLogisticsPackageInfo.setDeliveryManId(packageGoodsVo.getDeliveryManId());
                packageInfos.add(agedmallLogisticsPackageInfo);
            }
            agedMallOrderMapper.insertPackageBatch(packageInfos);
            agedMallOrderMapper.updateOrderByOrderSn(OrderStatusEnum.ORDER_DELIVER_GOODS.getOrderStatus(),LocalDateTime.now(),deliverGoodsVo.getSubOrderCode(),null);
        }

         return ResponseUtil.ok();
    }

    public Object returnGoods(ReturnGoodsVo returnGoodsVo) {
        AgedmallReturnGoodsRecord agedmallReturnGoodsRecord = new AgedmallReturnGoodsRecord();
        LitemallOrderExample litemallOrderExample = new LitemallOrderExample();
        litemallOrderExample.or().andOrderSnEqualTo(returnGoodsVo.getOrderSn());
        String goodsId = returnGoodsVo.getOrderGoodsId().toString();
        goodsId = goodsId.substring(1, goodsId.length() - 1);
        List<LitemallOrder> liteMallOrder = litemallOrderMapper.selectByExampleSelective(litemallOrderExample);
        agedmallReturnGoodsRecord.setUid(liteMallOrder.get(0).getUserId().longValue());
        agedmallReturnGoodsRecord.setOrderCode(returnGoodsVo.getOrderSn());
        agedmallReturnGoodsRecord.setReturnType(returnGoodsVo.getReturnType().byteValue());
        agedmallReturnGoodsRecord.setGoodsIds(goodsId);
        agedmallReturnGoodsRecord.setReason(returnGoodsVo.getReason());
        agedmallReturnGoodsRecord.setGetGoodsStyle(returnGoodsVo.getGetGoodsStyle().byteValue());
        agedmallReturnGoodsRecord.setDeliveryManId(returnGoodsVo.getDeliveryManId());
        agedmallReturnGoodsRecord.setLogisticsInfo(returnGoodsVo.getLogisticsInfo());
        agedmallReturnGoodsRecord.setAddTime(LocalDateTime.now());
        agedmallReturnGoodsRecord.setUpdateTime(LocalDateTime.now());
        //orderGoods状态变更
        List<Integer> goodsIdList = returnGoodsVo.getOrderGoodsId();
        for (Integer goodId : goodsIdList) {
            LitemallOrderGoods litemallOrderGoods = litemallOrderGoodsMapper.selectByPrimaryKey(goodId);
            litemallOrderGoods.setOrderGoodsStat(OrderStatusEnum.ORDER_RETURN_GOODS.getOrderStatus());
            litemallOrderGoodsMapper.updateByPrimaryKeySelective(litemallOrderGoods);
        }
        //保存退货记录
        agedmallReturnGoodsRecordMapper.insertSelective(agedmallReturnGoodsRecord);
        //修改订单退款金额
        LitemallOrder litemallOrder=liteMallOrder.get(0);
        litemallOrder.setAftersaleStatus((short)1);
        litemallOrderMapper.updateByPrimaryKey(litemallOrder);
        //售后相关信息
        String aftersaleSn=UUID.randomUUID().toString();
        for (int i = 0; i < returnGoodsVo.getOrderGoodsId().size(); i++) {
            LitemallAftersale litemallAftersale=new LitemallAftersale();
            litemallAftersale.setAftersaleSn(aftersaleSn);
            litemallAftersale.setOrderId(liteMallOrder.get(0).getId());
            litemallAftersale.setUserId(liteMallOrder.get(0).getUserId());
            litemallAftersale.setType(returnGoodsVo.getReturnType().shortValue());
            litemallAftersale.setReason(returnGoodsVo.getReason());
            litemallAftersale.setAmount(returnGoodsVo.getRefundAmount());
            litemallAftersale.setStatus((short)1);
            litemallAftersale.setAddTime(LocalDateTime.now());
            litemallAftersale.setUpdateTime(LocalDateTime.now());
            litemallAftersale.setOrderGoodsId(returnGoodsVo.getOrderGoodsId().get(i));
            litemallAftersaleMapper.insertSelective(litemallAftersale);
        }

        return ResponseUtil.ok();
    }

    public Object pickupGoods(PickupGoodsVo pickupGoodsVo) {
        LitemallAdmin admin = (LitemallAdmin) SecurityUtils.getSubject().getPrincipal();
        AgedmallPickupGoodsRecord ageMallPickupGoodsRecord = new AgedmallPickupGoodsRecord();
        LitemallOrderExample litemallOrderExample = new LitemallOrderExample();
        litemallOrderExample.or().andOrderSnEqualTo(pickupGoodsVo.getOrderSn());
        String orderGoodsId = pickupGoodsVo.getOrderGoodsId().toString();
        orderGoodsId = orderGoodsId.substring(1, orderGoodsId.length() - 1);
        String goodsId = pickupGoodsVo.getGoodsId().toString();
        goodsId = goodsId.substring(1, goodsId.length() - 1);
        List<LitemallOrder> liteMallOrder = litemallOrderMapper.selectByExampleSelective(litemallOrderExample);
        ageMallPickupGoodsRecord.setUid(liteMallOrder.get(0).getUserId().longValue());
        ageMallPickupGoodsRecord.setOrderCode(pickupGoodsVo.getOrderSn());
        ageMallPickupGoodsRecord.setGoodsIds(orderGoodsId);
        ageMallPickupGoodsRecord.setReason(pickupGoodsVo.getReason());
        ageMallPickupGoodsRecord.setGetGoodsStyle(pickupGoodsVo.getGetGoodsStyle().byteValue());
        ageMallPickupGoodsRecord.setDeliveryManId(pickupGoodsVo.getDeliveryManId());
        ageMallPickupGoodsRecord.setMaintainManId(pickupGoodsVo.getMaintainManId());
        ageMallPickupGoodsRecord.setLogisticsInfo(pickupGoodsVo.getLogisticsInfo());
        ageMallPickupGoodsRecord.setAddTime(LocalDateTime.now());
        ageMallPickupGoodsRecord.setUpdateTime(LocalDateTime.now());

        //增加包裹表
        AgedmallLogisticsPackageInfo agedmallLogisticsPackageInfo = new AgedmallLogisticsPackageInfo();
        agedmallLogisticsPackageInfo.setOrderCode(pickupGoodsVo.getOrderSn());
        agedmallLogisticsPackageInfo.setOrderGoodsIds(orderGoodsId);
        agedmallLogisticsPackageInfo.setGoodsIds(goodsId);
        agedmallLogisticsPackageInfo.setPackageType((byte) 2);
        agedmallLogisticsPackageInfo.setShipDictName(pickupGoodsVo.getShipDictName());
        agedmallLogisticsPackageInfo.setShipSn(pickupGoodsVo.getShipSn());
        agedmallLogisticsPackageInfo.setShipChannel(pickupGoodsVo.getShipChannel());
        agedmallLogisticsPackageInfo.setShipTime(LocalDateTime.now());
        agedmallLogisticsPackageInfo.setOptUid(admin.getId());
        agedmallLogisticsPackageInfo.setAddTime(LocalDateTime.now());
        agedmallLogisticsPackageInfo.setUpdateTime(LocalDateTime.now());
        agedmallLogisticsPackageInfo.setDeliveryManId(pickupGoodsVo.getDeliveryManId());
        //取货状态码
        agedmallLogisticsPackageInfo.setShipStatus(OrderStatusEnum.ORDER_PICKUP_GOODS.getOrderStatus());
        //orderGoods状态变更
        List<Integer> goodsIdList = pickupGoodsVo.getOrderGoodsId();
        for (Integer goodId : goodsIdList) {
            LitemallOrderGoods litemallOrderGoods = litemallOrderGoodsMapper.selectByPrimaryKey(goodId);
            litemallOrderGoods.setOrderGoodsStat(OrderStatusEnum.ORDER_PICKUP_GOODS.getOrderStatus());
            litemallOrderGoodsMapper.updateByPrimaryKeySelective(litemallOrderGoods);
        }
        //保存退货记录
        agedmallReturnGoodsRecord.insertSelective(ageMallPickupGoodsRecord);
        //保存包裹表
        agedmallLogisticsPackageInfoMapper.insertSelective(agedmallLogisticsPackageInfo);
        return ResponseUtil.ok();
    }


    public Object pickupGoodsReceiving(String orderCode,Integer isExtract) {
       //不是自提
       if(isExtract==0){
           AgedmallLogisticsPackageInfoExample agedmallLogisticsPackageInfoExample=new AgedmallLogisticsPackageInfoExample();
           agedmallLogisticsPackageInfoExample.or().andSubOrderCodeEqualTo(orderCode);
           AgedmallLogisticsPackageInfo agedmallLogisticsPackageInfo=new AgedmallLogisticsPackageInfo();
           agedmallLogisticsPackageInfo.setShipStatus(1);
           agedmallLogisticsPackageInfoMapper.updateByExampleSelective(agedmallLogisticsPackageInfo,agedmallLogisticsPackageInfoExample);
       }
       //是自提
       else{
           LitemallOrderExample litemallOrderExample=new LitemallOrderExample();
           litemallOrderExample.or().andOrderSnEqualTo(orderCode);
           LitemallOrder litemallOrder=new LitemallOrder();
           litemallOrder.setExtractTime(LocalDateTime.now());
           litemallOrderMapper.updateByExampleSelective(litemallOrder,litemallOrderExample);
       }

        return ResponseUtil.ok();
    }

    public Object refund(Refund refund) {
        LitemallAdmin admin = (LitemallAdmin) SecurityUtils.getSubject().getPrincipal();
        LitemallAftersaleExample litemallAftersaleExample=new LitemallAftersaleExample();
        litemallAftersaleExample.or().andOrderIdEqualTo(refund.getSubOrderId());
        LitemallAftersale litemallAftersale=new LitemallAftersale();
        litemallAftersale.setStatus(refund.getStatus().shortValue());
        litemallAftersale.setHandleTime(LocalDateTime.now());

        Integer type=refund.getType();
        if(type==1){
            litemallAftersale.setRejectReason(refund.getReason());
        }
        if(type==4){
            GoodsReturnRefundDto goodsReturnRefundDto=new GoodsReturnRefundDto();
            goodsReturnRefundDto.setChannel(refund.getChannel());
            goodsReturnRefundDto.setRefundCode(refund.getRefundCode());
            goodsReturnRefundDto.setSubOrderCode(refund.getSubOrderCode());
            goodsReturnRefundDto.setGoodsId(refund.getGoodsId());
            agedMallOrderMapper.updateLeaseGoodsReturnRefundByOrderId(goodsReturnRefundDto);
            //更新agedmall_deduct_detail
            DeductDetailVo deductDetailVo=new DeductDetailVo();
            deductDetailVo.setDeleted(true);
            deductDetailVo.setType("GET");
            deductDetailVo.setSubOrderSn(refund.getSubOrderCode());
            deductDetailVo.setGoodsId(refund.getGoodsId());
            agedMallOrderMapper.updatedeductDetailBySubOrderSn(deductDetailVo);
            //更新agedmall_user_info
            AgedmallUserInfoVo  agedmallUserInfoVo=new AgedmallUserInfoVo();
            agedmallUserInfoVo.setType("GET");
            agedmallUserInfoVo.setSubOrderSn(refund.getSubOrderCode());
            agedmallUserInfoVo.setGoodsId(refund.getGoodsId());
            agedmallUserInfoVo.setUserId(admin.getId());
            agedMallOrderMapper.updateAgedmallUserInfoBySubOrderSn(agedmallUserInfoVo);
        }
        if(type==6){
            GoodsReturnRefundDto goodsReturnRefundDto=new GoodsReturnRefundDto();
            goodsReturnRefundDto.setLogisticsInfo(refund.getLogisticsInfo());
            goodsReturnRefundDto.setSubOrderCode(refund.getSubOrderCode());
            goodsReturnRefundDto.setGoodsId(refund.getGoodsId());
            agedMallOrderMapper.updateLeaseGoodsReturnRefundByOrderId(goodsReturnRefundDto);
        }
        if(type==7){
            GoodsReturnRefundDto goodsReturnRefundDto=new GoodsReturnRefundDto();
            goodsReturnRefundDto.setEndTime(LocalDateTime.now());
            goodsReturnRefundDto.setSubOrderCode(refund.getSubOrderCode());
            goodsReturnRefundDto.setGoodsId(refund.getGoodsId());
            agedMallOrderMapper.updateLeaseGoodsReturnRefundByOrderId(goodsReturnRefundDto);
        }
        if(type==8){
            GoodsReturnRefundDto goodsReturnRefundDto=new GoodsReturnRefundDto();
            goodsReturnRefundDto.setChannel(refund.getChannel());
            goodsReturnRefundDto.setRefundCode(refund.getRefundCode());
            goodsReturnRefundDto.setSubOrderCode(refund.getSubOrderCode());
            goodsReturnRefundDto.setGoodsId(refund.getGoodsId());
            agedMallOrderMapper.updateLeaseGoodsReturnRefundByOrderId(goodsReturnRefundDto);
            //更新agedmall_deduct_detail
            DeductDetailVo deductDetailVo=new DeductDetailVo();
            deductDetailVo.setDeleted(true);
            deductDetailVo.setType("GET");
            deductDetailVo.setSubOrderSn(refund.getSubOrderCode());
            deductDetailVo.setGoodsId(refund.getGoodsId());
            agedMallOrderMapper.updatedeductDetailBySubOrderSn(deductDetailVo);
            //更新agedmall_user_info
            AgedmallUserInfoVo  agedmallUserInfoVo=new AgedmallUserInfoVo();
            agedmallUserInfoVo.setType("GET");
            agedmallUserInfoVo.setSubOrderSn(refund.getSubOrderCode());
            agedmallUserInfoVo.setGoodsId(refund.getGoodsId());
            agedmallUserInfoVo.setUserId(admin.getId());
            agedMallOrderMapper.updateAgedmallUserInfoBySubOrderSn(agedmallUserInfoVo);
        }
        if(type==10){
            GoodsReturnRefundDto goodsReturnRefundDto=new GoodsReturnRefundDto();
            goodsReturnRefundDto.setLogisticsInfo(refund.getLogisticsInfo());
            goodsReturnRefundDto.setSubOrderCode(refund.getSubOrderCode());
            goodsReturnRefundDto.setGoodsId(refund.getGoodsId());
            agedMallOrderMapper.updateLeaseGoodsReturnRefundByOrderId(goodsReturnRefundDto);
        }
        if(type==11){
            GoodsReturnRefundDto goodsReturnRefundDto=new GoodsReturnRefundDto();
            goodsReturnRefundDto.setSendLogisticsInfo(refund.getSendLogisticsInfo());
            goodsReturnRefundDto.setEndTime(LocalDateTime.now());
            goodsReturnRefundDto.setSendTime(LocalDateTime.now());
            goodsReturnRefundDto.setSubOrderCode(refund.getSubOrderCode());
            goodsReturnRefundDto.setGoodsId(refund.getGoodsId());
            agedMallOrderMapper.updateLeaseGoodsReturnRefundByOrderId(goodsReturnRefundDto);
        }
        if(type==12){
            GoodsReturnRefundDto goodsReturnRefundDto=new GoodsReturnRefundDto();
            goodsReturnRefundDto.setSendArriveTime(LocalDateTime.now());
            goodsReturnRefundDto.setSubOrderCode(refund.getSubOrderCode());
            goodsReturnRefundDto.setGoodsId(refund.getGoodsId());
            agedMallOrderMapper.updateLeaseGoodsReturnRefundByOrderId(goodsReturnRefundDto);
            //更新agedmall_deduct_detail
            DeductDetailVo deductDetailVo=new DeductDetailVo();
            deductDetailVo.setDeleted(true);
            deductDetailVo.setType("GET");
            deductDetailVo.setSubOrderSn(refund.getSubOrderCode());
            deductDetailVo.setGoodsId(refund.getGoodsId());
            agedMallOrderMapper.updatedeductDetailBySubOrderSn(deductDetailVo);
            //更新agedmall_user_info
            AgedmallUserInfoVo  agedmallUserInfoVo=new AgedmallUserInfoVo();
            agedmallUserInfoVo.setType("GET");
            agedmallUserInfoVo.setSubOrderSn(refund.getSubOrderCode());
            agedmallUserInfoVo.setGoodsId(refund.getGoodsId());
            agedmallUserInfoVo.setUserId(admin.getId());
            agedMallOrderMapper.updateAgedmallUserInfoBySubOrderSn(agedmallUserInfoVo);
        }
        if(type==14){
            GoodsReturnRefundDto goodsReturnRefundDto=new GoodsReturnRefundDto();
            goodsReturnRefundDto.setDeliveryManId(1);
            goodsReturnRefundDto.setSendDeliveryManId(1);
            goodsReturnRefundDto.setSendTime(LocalDateTime.now());
            goodsReturnRefundDto.setSubOrderCode(refund.getSubOrderCode());
            goodsReturnRefundDto.setGoodsId(refund.getGoodsId());
            agedMallOrderMapper.updateLeaseGoodsReturnRefundByOrderId(goodsReturnRefundDto);
        }
        if(type==15){
            GoodsReturnRefundDto goodsReturnRefundDto=new GoodsReturnRefundDto();
            goodsReturnRefundDto.setSendArriveTime(LocalDateTime.now());
            goodsReturnRefundDto.setEndTime(LocalDateTime.now());
            goodsReturnRefundDto.setSubOrderCode(refund.getSubOrderCode());
            goodsReturnRefundDto.setGoodsId(refund.getGoodsId());
            agedMallOrderMapper.updateLeaseGoodsReturnRefundByOrderId(goodsReturnRefundDto);
            //更新agedmall_deduct_detail
            DeductDetailVo deductDetailVo=new DeductDetailVo();
            deductDetailVo.setDeleted(true);
            deductDetailVo.setType("GET");
            deductDetailVo.setSubOrderSn(refund.getSubOrderCode());
            deductDetailVo.setGoodsId(refund.getGoodsId());
            agedMallOrderMapper.updatedeductDetailBySubOrderSn(deductDetailVo);
            //更新agedmall_user_info
            AgedmallUserInfoVo  agedmallUserInfoVo=new AgedmallUserInfoVo();
            agedmallUserInfoVo.setType("GET");
            agedmallUserInfoVo.setSubOrderSn(refund.getSubOrderCode());
            agedmallUserInfoVo.setGoodsId(refund.getGoodsId());
            agedmallUserInfoVo.setUserId(admin.getId());
            agedMallOrderMapper.updateAgedmallUserInfoBySubOrderSn(agedmallUserInfoVo);
        }
        litemallAftersaleMapper.updateByExampleSelective(litemallAftersale,litemallAftersaleExample);
        return ResponseUtil.ok();
    }

    public Object leaseReturnConfirm(LeaseReturnConfirmVo leaseReturnConfirmVo){
        agedMallOrderMapper.updateLeaseGoodsReturnByOrderId(LocalDateTime.now(),LocalDateTime.now(),leaseReturnConfirmVo.getOrderId(),leaseReturnConfirmVo.getSubOrderId());
        if(leaseReturnConfirmVo.getType()==0){
        LitemallOrderExample litemallOrderExample=new LitemallOrderExample();
        litemallOrderExample.or().andOrderSnEqualTo(leaseReturnConfirmVo.getSubOrderCode());
        LitemallOrder litemallOrder=new LitemallOrder();
        litemallOrder.setOrderStatus(OrderStatusEnum.ORDER_LEASE_RENTURN_CONFIRM.getOrderStatus().shortValue());
        litemallOrderMapper.updateByExampleSelective(litemallOrder,litemallOrderExample);
        }
        return ResponseUtil.ok();
    }

    public Object modifyRefundAmount(ModifyRefundMoneyVo modifyRefundMoneyVo) {
        LitemallOrder litemallOrder = litemallOrderMapper.selectByPrimaryKey(modifyRefundMoneyVo.getOrderId());
        litemallOrder.setRefundAmount(modifyRefundMoneyVo.getRefundAmount());
        litemallOrderMapper.updateByPrimaryKeySelective(litemallOrder);
        return ResponseUtil.ok();
    }

    public Object receivedReturnGoods(List<Integer> orderGoodsId) {
        for (Integer goodId : orderGoodsId) {
            LitemallOrderGoods litemallOrderGoods = litemallOrderGoodsMapper.selectByPrimaryKey(goodId);
            litemallOrderGoods.setOrderGoodsStat(OrderStatusEnum.ORDER_RETURN_GOODS_FINISH.getOrderStatus());
            litemallOrderGoodsMapper.updateByPrimaryKeySelective(litemallOrderGoods);
        }
        return ResponseUtil.ok();
    }


    public Object getAfterSaleOrderDetail(Integer id) {
        LitemallOrder litemallOrder = litemallOrderMapper.selectByPrimaryKeySelective(id);
        OrderDetailBackVo orderDetailBackVo = new OrderDetailBackVo();
        BeanUtils.copyProperties(litemallOrder, orderDetailBackVo);
        Map<Integer, Object> packageMap = new LinkedHashMap<>();
        //售后物品列表
        List<OrderGoodsBackVo> liteMallOrderGoods = agedMallOrderMapper.selectAfterSaleGoodsByOrderId(id);
        orderDetailBackVo.setGoodsList(liteMallOrderGoods);
        List<LitemallAftersale> afterSales=agedMallOrderMapper.selectAfterSaleByOrderId(id);
        orderDetailBackVo.setAftersalesList(afterSales);

        //分包裹
        AgedmallLogisticsPackageInfoExample agedmallLogisticsPackageInfoExample = new AgedmallLogisticsPackageInfoExample();
        agedmallLogisticsPackageInfoExample.or().andOrderCodeEqualTo(litemallOrderMapper.selectByPrimaryKey(litemallOrder.getId()).getOrderSn());
        List<AgedmallLogisticsPackageInfo> agedMallLogisticsPackageInfoList = agedmallLogisticsPackageInfoMapper.selectByExample(agedmallLogisticsPackageInfoExample);
        for (int k = 0; k < agedMallLogisticsPackageInfoList.size(); k++) {
            List<OrderGoodsBackVo> packageGoods = new ArrayList<>();
            OrderPackageVo orderPackageVo = new OrderPackageVo();
            String[] orderGoodsId;
            orderGoodsId = agedMallLogisticsPackageInfoList.get(k).getOrderGoodsIds().split(",");
            for (OrderGoodsBackVo orderGoodsBackVo : liteMallOrderGoods) {
                if (orderGoodsId.length > 0 && Arrays.asList(orderGoodsId).contains(orderGoodsBackVo.getId().toString())) {
                    AgedmallLogisticsPackageInfo agedmallLogisticsPackageInfo = agedMallLogisticsPackageInfoList.get(k);
                    if (agedmallLogisticsPackageInfo != null) {
                        orderGoodsBackVo.setShipDictName(agedmallLogisticsPackageInfo.getShipDictName());
                        orderGoodsBackVo.setShipSn(agedmallLogisticsPackageInfo.getShipSn());
                        orderGoodsBackVo.setShipChannel(agedmallLogisticsPackageInfo.getShipChannel());
                        orderGoodsBackVo.setShipStatus(agedmallLogisticsPackageInfo.getShipStatus());
                        orderPackageVo.setShipDictName(agedmallLogisticsPackageInfo.getShipDictName());
                        orderPackageVo.setShipSn(agedmallLogisticsPackageInfo.getShipSn());
                        orderPackageVo.setShipChannel(agedmallLogisticsPackageInfo.getShipChannel());
                        orderPackageVo.setShipStatus(agedmallLogisticsPackageInfo.getShipStatus());
                        orderPackageVo.setShipTime(agedmallLogisticsPackageInfo.getShipTime());
                        orderPackageVo.setPackageType(agedmallLogisticsPackageInfo.getPackageType());
                    }
                    packageGoods.add(orderGoodsBackVo);
                }
            }
            orderPackageVo.setGoodsBackVos(packageGoods);
            packageMap.put(k, orderPackageVo);
        }

        List<OrderLogisticsMessage> orderLogisticsMessageList =new ArrayList<>();
        for (int i = 0; i < liteMallOrderGoods.size(); i++) {
            OrderLogisticsMessage orderLogisticsMessage=new OrderLogisticsMessage();
            AgedmallStore agedmallStore=agedmallStoreMapper.selectByPrimaryKey(liteMallOrderGoods.get(i).getStoreId().longValue());
            orderLogisticsMessage.setStoreId(agedmallStore.getId().intValue());
            orderLogisticsMessage.setStoreName(agedmallStore.getStoreName());
            orderLogisticsMessage.setShipDictName(liteMallOrderGoods.get(i).getShipDictName());
            orderLogisticsMessage.setShipChannel(liteMallOrderGoods.get(i).getShipChannel());
            orderLogisticsMessage.setShipSn(liteMallOrderGoods.get(i).getShipSn());
            orderLogisticsMessage.setAddress(agedmallStore.getProvinceName()+agedmallStore.getCityName()+agedmallStore.getAreaName()+agedmallStore.getAddressDetail());
            orderLogisticsMessageList.add(orderLogisticsMessage);
        }
        orderDetailBackVo.setOrderLogisticsMessages(orderLogisticsMessageList);
        orderDetailBackVo.setPackageMap(packageMap);
        return ResponseUtil.ok(orderDetailBackVo);
    }

    public Object updateOrderSetting(Map<String,String> systemMap){
        for (Map.Entry<String, String> entry : systemMap.entrySet()) {
            LitemallSystem litemallSystem=agedMallOrderMapper.selectSystemConfigByKeyName(entry.getKey());
            if(litemallSystem!=null){
                litemallSystem.setKeyValue(entry.getValue());
                litemallSystemMapper.updateByPrimaryKey(litemallSystem);
            }
            else{
                litemallSystem=new LitemallSystem();
                litemallSystem.setKeyName(entry.getKey());
                litemallSystem.setKeyValue(entry.getValue());
                litemallSystem.setAddTime(LocalDateTime.now());
                litemallSystem.setUpdateTime(LocalDateTime.now());
                litemallSystemMapper.insertSelective(litemallSystem);
            }
        }
        return ResponseUtil.ok();
    }

    public Object getOrderSetting(List<String> systemSetting){
        List<LitemallSystem> systemList=new ArrayList<>();
        for (String systemName:systemSetting) {
            LitemallSystem litemallSystem=agedMallOrderMapper.selectSystemConfigByKeyName(systemName);
            if(litemallSystem!=null){
                systemList.add(litemallSystem);
            }
        }
        return ResponseUtil.ok(systemList);
    }

    public Object getOrderStatusList(){
        Field[] fields = OrderUtil.class.getDeclaredFields();
        Map<Integer,String> orderStatusList=new HashMap<>();
        for(Field field :fields){

            try {
                String num=field.get(field.getName()).toString();
                if(!num.equals("501")&&!num.equals("502")&&!num.equals("403")&&!num.equals("404")){
                    LitemallOrder litemallOrder=new LitemallOrder();
                    litemallOrder.setOrderStatus(Short.parseShort(num));
                    String text = OrderUtil.orderStatusText(litemallOrder);
                    orderStatusList.put(Integer.parseInt(num),text);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

        }
        return ResponseUtil.ok(orderStatusList);
    }


       public Object getStoresByCity(Integer provinceId,Integer cityId){
           List<AgedmallStore> agedmallStores = agedMallOrderMapper.selectStoreListByCityId(provinceId, cityId);
           return ResponseUtil.ok(agedmallStores);
       }

}
*/
